package com.orifine.orm;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.sql.DataSource;

import com.orifine.orm.mysql.MySqlScriptGenerator;
import com.orifine.utils.ScannerUtils;

public class SqlScriptFactory {
	private String databaseName = null;
	private Set<Class<?>> tableClasses = new HashSet<Class<?>>();
	private DataSource dataSource;
	private SqlScriptGenerator sqlScriptGenerator;
	private int infoMaxSize = 20;
//	private Internationalization properties;
	
	public String getDatabaseName() {
		return databaseName;
	}

	public void setDatabaseName(String databaseName) {
		this.databaseName = databaseName;
	}

	public Set<Class<?>> getTableClasses() {
		return tableClasses;
	}

	public void setTableClasses(Set<Class<?>> tableClasses) {
		this.tableClasses = tableClasses;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
//	public Internationalization getProperties() {
//		return properties;
//	}
//
//	public void setProperties(String propertiesClassPath) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//		this.properties = ((Class<Internationalization>)Class.forName(propertiesClassPath)).newInstance();
//	}

	public void execute(){
		sqlScriptGenerator.setDatabase(databaseName);
		sqlScriptGenerator.addTables(tableClasses);
		
		if(dataSource == null){
			sqlScriptGenerator.sqlList();
			return;
		}
		
		Connection connection = null;
		Statement statement = null;
		try {
			connection = dataSource.getConnection();
			statement = connection.createStatement();
			List<String> sqlList = sqlScriptGenerator.sqlList();
			for(String sql : sqlList){
				try{
					statement.execute(sql);
					System.out.println("SUCCES:"+sql);
				} catch (SQLException e) {
					System.err.println("ERROR:"+sql);
					System.err.println("MESSAGE:"+e.getMessage());
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		try {
			if(statement != null && !statement.isClosed()){
				statement.close();
			}
			if(connection != null && !connection.isClosed()){
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		MySqlScriptGenerator generator = new MySqlScriptGenerator();
		generator.setDatabase("test");
		generator.setInfoMaxSize(20);
		generator.addTables(ScannerUtils.getClasses("com.bowos.zmado.model"));
		for(String sql : generator.sqlList()){
			System.out.println(sql);
		}
	}

	public SqlScriptGenerator getSqlScriptGenerator() {
		return sqlScriptGenerator;
	}

	public void setSqlScriptGenerator(SqlScriptGenerator sqlScriptGenerator) {
		this.sqlScriptGenerator = sqlScriptGenerator;
	}

	public void setInfoMaxSize(int infoMaxSize) {
		this.infoMaxSize = infoMaxSize;
	}

	public int getInfoMaxSize() {
		return infoMaxSize;
	}
}
